<?php
/*
  [EWEISHOP] Copyright(C) 2017 Elinkint Inc.
  This is NOT a freeware, use is subject to license terms
   
  PaymentNotify.php 2018-05-24 18:43 cunx $
   
*/

namespace common\library\wechat\payment;

use common\component\wechat\Wechat;
use common\library\wechat\WechatApi;
use EasyWeChat\Kernel\Exceptions\Exception;

/**
 * 支付/退款/扫码 通知
 * Class PaymentNotify
 * @package common\library\wechat\payment
 * @author cunx
 */
class PaymentNotify extends WechatApi
{
    public static $apiType = 'payment';
    static $paymentId = 0;
    /**
     * 支付通知
     * @param \Closure $callback    回调函数：function ($message, $fail)
     * ------------------------------------------------------------------------
     * 回调函数：function ($message, $fail)
     * @args $message   微信推送过来的通知信息，为一个数组
     * @args $fail      触发 `$fail`函数，则返回"通信错误"到微信服务器，微信会稍后重试再通知。
     * ------------------------------------------------------------------------
     * @return array|bool|void
     * @author cunx
     */
    public static function payNotify(\Closure $callback){
        try{
            $response = self::getInstance()->handlePaidNotify($callback);
        }catch(Exception $response){}
        return Wechat::apiError($response);
    }

    /**
     * 退款通知
     * @param \Closure $callback    回调函数：function ($message, $fail)
     * ------------------------------------------------------------------------
     * 回调函数：function ($message, $fail)
     * @args $message   微信推送过来的通知信息，为一个数组
     * @args $fail      触发 `$fail`函数，则返回"通信错误"到微信服务器，微信会稍后重试再通知。
     * ------------------------------------------------------------------------
     * @return array|bool|void
     * @author cunx
     */
    public static function refundedNotify(\Closure $callback){
        try{
            $response = self::getInstance()->handleRefundedNotify($callback);
        }catch(Exception $response){}
        return Wechat::apiError($response);
    }

    /**
     * 扫码通知
     * @param \Closure $callback    回调函数：function ($message, $fail, $alert)
     * ------------------------------------------------------------------------
     * 回调函数：function ($message, $fail, $alert)
     * @args $message   微信推送过来的通知信息，为一个数组
     * @args $fail      触发 `$fail`函数，则返回"通信错误"到微信服务器，微信会稍后重试再通知。
     * @args $alert     如果触发`$alert`函数，会返回"业务错误"到微信服务器：$alert('商品已售空');
     * ------------------------------------------------------------------------
     * @return array|bool|void
     * @author cunx
     */
    public static function scannedNotify(\Closure $callback){
        try{
            $response = self::getInstance()->handleScannedNotify($callback);
        }catch(Exception $response){}
        return Wechat::apiError($response);
    }
}